package com.jrx.anytxn.transaction.job.waiveAutoPayment.step;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.customer.dto.customer.CustomerRes;
import com.jrx.anytxn.customer.service.ICustomerService;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.entity.PrProductTable;
import com.jrx.anytxn.param.service.product.IProductTableService;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.constant.TransErrMsgConstant;
import com.jrx.anytxn.transaction.entity.*;
import com.jrx.anytxn.transaction.job.waiveAutoPayment.bean.WaivePaymentLogBean;
import com.jrx.anytxn.transaction.mapper.ext.*;
import com.jrx.anytxn.transaction.service.IWaivePaymentLogService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 贴息还款批扣准备processor
 *
 * @author : zhenghaopu
 * 2019/11/22
 */
@Service
public class WaiveAutoPaymentPreProcessor implements ItemProcessor<AmWaiveRepaymentPlanInfo, WaivePaymentLogBean> {

    private Logger logger = LoggerFactory.getLogger(WaiveAutoPaymentPreProcessor.class);

    @Autowired
    private IOrganizationTableService organizationTableService;


    @Autowired
    private ExtAmLoanAccountInfoMapper extAmLoanAccountInfoMapper;


    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IProductTableService productTableService;

    @Autowired
    private IWaivePaymentLogService waivePaymentLogService;

    @Autowired
    private ExtAmLoanMainInfoMapper extAmLoanMainInfoMapper;

    @Autowired
    private ExtTlWaivePaymentLogMapper extTlWaivePaymentLogMapper;





    @Override
    public WaivePaymentLogBean process(AmWaiveRepaymentPlanInfo planInfo) throws Exception {
        WaivePaymentLogBean saveBean = new WaivePaymentLogBean();

        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date businessDate = org.getNextProcessingDate();




        //查询客戶下处理中的还款流水
        List<String> sumStatusList = Arrays.asList(TransBizConstant.PAYMENT_LOG_SUM_STATUS_O,
                TransBizConstant.PAYMENT_LOG_SUM_STATUS_T, TransBizConstant.PAYMENT_LOG_SUM_STATUS_D);
        List<TlWaivePaymentLog> tlPaymentLogList =
                extTlWaivePaymentLogMapper.selectListBySumStatusAndPayTypeForBatch(planInfo.getCustomerId(),
                        planInfo.getChannel(),TransBizConstant.REPAYMENT_TYPE_BILLPAY,sumStatusList);
        if(CollectionUtils.isNotEmpty(tlPaymentLogList)){
            return null;
        }

        AmLoanAccountInfo loanAccountInfo = Optional.ofNullable(extAmLoanAccountInfoMapper.selectByLoanId(planInfo.getLoanId(),
                planInfo.getCustomerId(), planInfo.getTenantId()))
                .orElseThrow(() -> {
                    logger.error("订单交易账户为空，loanId:{}", planInfo.getLoanId());
                    return new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getUserTips());
                });

        AmLoanMainInfo loanMainInfo = extAmLoanMainInfoMapper.selectByLoanIdAndCusId(planInfo.getLoanId(),
                planInfo.getCustomerId(), planInfo.getTenantId());
        CustomerRes customerRes = customerService.getCustomerByChannelAndCustomerIdAndTensnId(loanAccountInfo.getChannel(),
                loanAccountInfo.getCustomerId(),loanAccountInfo.getTenantId());
        PrProductTable prProductTable = productTableService.findProByProIdAndOrgId(loanMainInfo.getProductId(), loanMainInfo.getOrganizationId(), loanMainInfo.getTenantId());
        TlWaivePaymentLog log = waivePaymentLogService.buildTlWaivePaymentLog(TransBizConstant.REPAYMENT_TYPE_WAIVEAUTO,customerRes
                ,planInfo.getInterestAmount(),businessDate,prProductTable);
        TlWaivePaymentMapping tlWaivePaymentMapping = waivePaymentLogService.biudTlWaivePaymentMapping(loanMainInfo.getCustomerId(),log.getSeqNo()
                ,loanMainInfo.getLoanId(),loanMainInfo.getTenantId());
        saveBean.setTlPaymentMapping(tlWaivePaymentMapping);
        saveBean.setTlWaivePaymentLog(log);
        return saveBean;
    }
}
